package com.springboot2.jpa.service;

import com.springboot2.jpa.bean.*;
import com.springboot2.jpa.dao.ArticleRepository;
import com.springboot2.jpa.dao.AuthorRepository;
import com.springboot2.jpa.dao.BookRepository;
import com.springboot2.jpa.dao.UserRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class UserService {
    @Resource
    private UserRepository userRepository;

    @Resource
    private BookRepository bookRepository;

    @Resource
    private AuthorRepository authorRepository;

    @Resource
    private ArticleRepository articleRepository;

    @Resource
    private MongoTemplate mongoTemplate;

    public void save() {
        User user = new User();
        user.setUsername("yangmiao");
        user.setPassword("123456");

        mongoTemplate.save(user);
    }

    public List<User> query() {
        return userRepository.findAllByUsername("yangmiao");
    }

    public List saveCategory() {
        BookCategory categoryOne = new BookCategory();
        categoryOne.setName("category1");

        Set books = new HashSet<Book>() {{
            add(new Book("Book One"));
            add(new Book("Book Two"));
            add(new Book("Book Three"));
        }};
        categoryOne.setBooks(books);

        BookCategory categoryTwo = new BookCategory();
        categoryTwo.setName("category2");
        Set bookBs = new HashSet<Book>() {{
            add(new Book("Book Four"));
            add(new Book("Book Five"));
            add(new Book("Book Six"));
        }};
        categoryTwo.setBooks(bookBs);

        Set allBooks = new HashSet();

        allBooks.add(categoryOne);
        allBooks.add(categoryTwo);

        return bookRepository.saveAll(allBooks);

    }

    public List queryBook() {
        return bookRepository.findAll();
    }


    public Book saveBook() {
        return bookRepository.save(new Book("测试书籍"));
    }

    public Article saveArticle() {
        Author author = authorRepository.getOne(1l);
        return articleRepository.save(new Article("标题4", "内容4", author));
    }


    public Author saveAuthor() {
        Author author = new Author();
        author.setName("杨淼");

        List<Article> list = new ArrayList<Article>() {{
            add(new Article("标题1", "内容1", author));
            add(new Article("标题2", "内容2", author));
            add(new Article("标题3", "内容3", author));
        }};
        author.setArticleList(list);

        return authorRepository.save(author);
    }

    public Object queryAuthor() {
        return authorRepository.findAll();
    }

    public Object queryArticle() {
        return articleRepository.findAll();
    }

    public Object queryArticlePage() {
        Page<Article> page = articleRepository.findBytitle(1, PageRequest.of(0, 10));
        System.out.println(page);
        return page;
    }

    public Object queryArticleById() {
        return articleRepository.queryAllById(1);
    }
}
